package com.syni.mdd.yhd.common.all.controller.protect;

/**
 * @className BmsBusinessController
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/15 14:25
 * @VERSION 1.0
 **/

import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.sjb.common.component.utils.common.utils.RequestHeaderData;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.entity.bo.user.CheckUserBo;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.yhd.common.all.service.BmsReportErrorService;
import com.syni.mdd.yhd.common.all.utils.RequestParamUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/reportError")
public class BmsReportErrorController {
    @Autowired
    BmsReportErrorService bmsReportErrorService;

    @PostMapping("/submitError")
    @CheckParams(params = {"error_type","business_id"})
    public Map<String,Object> submitError(@RequestBody Map<String,Object> paramsMap){
        Integer userId = 0;
        CheckUserBo checkUserBo = RequestParamUtils.checkUserBo();
        //默认使用设备标识
        if(checkUserBo.getCheckStatus().equals(ResultCode.CODE_NORMAL)){
            userId = Integer.parseInt(checkUserBo.getUserId());
        }
        //默认游客报错
        int client = Integer.parseInt(paramsMap.get("client").toString());

        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer errorType = Integer.parseInt(paramsMap.get("error_type").toString());
        String errorContent = "";
        if(paramsMap.containsKey("error_content")){
            errorContent = paramsMap.get("error_content").toString();
        }
        if(errorContent.length() > Constants.MAX_CONTENT_LENG){
            return ResultMapHelper.result(ResultCode.CODE_BUSINESS_REPORT_ERROR_LONG,"内容过长");
        }
        Integer createWay = BmsCommonService.clientMap.get(client);
        return bmsReportErrorService.recordReportError(businessId,userId,
                errorType,errorContent,createWay);
    }
}
